package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.mapper.select.nopaging.CheckBoxMapper;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.vo.out.chart.ChartVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.chart.HomepageNeedsVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.drop_down_box.DropDownBoxVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.linkage.equipment.EquipmentCategoryVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.linkage.user.UserLinkageVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import edu.lsnu.equipment_management_system.service.CheckBoxService;
import edu.lsnu.equipment_management_system.util.DateUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author mzy
 * @Description  饼状图、柱状图、下拉菜单、二级联动、三级联动
 * @date 2018/11/16
 */
@RestController
public class CheckBoxController {
    @Autowired
    CheckBoxMapper checkBoxMapper;
    @Autowired
    CheckBoxService checkBoxService;
    /**
     * @Description 获取设备类别的下拉框
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/drop_down_box/equipment/category")
    public Object getEquipmentCategoryDropDownBox() {
        List<DropDownBoxVo> equipmentCategory = checkBoxMapper.getEquipmentCategoryDropDownBoxVo();
        if (null == equipmentCategory) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentCategory, "操作成功");
        }

    }

    /**
     * @Description 获取研究室的下拉菜单
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/drop_down_box/researchRoom/category")
    public Object getResearchRoomDropDownBox() {
        List<DropDownBoxVo> researchRoom = checkBoxMapper.getResearchRoomDropDownBoxVo();
        if (null == researchRoom) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, researchRoom, "操作成功");
        }

    }

    /**
     * @Description 获取存放场所的下拉菜单
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/drop_down_box/storagePlace/category")
    public Object getStoragePlaceDropDownBox() {
        List<DropDownBoxVo> storagePlace = checkBoxMapper.getStoragePlaceDropDownBoxVo();
        if (null == storagePlace) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, storagePlace, "操作成功");
        }
    }

    /**
     * @Description 获取利用率模板的下拉菜单
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/drop_down_box/utilizationTemplate/category")
    public Object getUtilizationTemplateDropDownBox() {
        List<DropDownBoxVo> utilizationTemplate = checkBoxMapper.getUtilizationTemplateDropDownBoxVo();
        if (null == utilizationTemplate) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, utilizationTemplate, "操作成功");
        }
    }


    /**
     * @Description 普通用户：主页需要的饼状图和柱状图
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   Ojbect
     * @params  []
     */
    @GetMapping("/api/user/index/chart")
    public Object userHomeChart(HttpSession session) {
        // 注意要从session中取出userNo.
        UserVo userVo = (UserVo) session.getAttribute("user");
        String userNo = userVo.getUserNo();

        List<ChartVo> histogram = checkBoxMapper.getUserHistogramChartVo(userNo); // 50830025
        List<ChartVo> pieChart = checkBoxMapper.getUserPieChartVo(userNo);

        HomepageNeedsVo chart = new HomepageNeedsVo(histogram, pieChart);

        if (null == chart) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, chart, "操作成功");
        }
    }

    /**
     * @Description 管理员：主页需要的饼状图和柱状图
     * @author mzy
     * @time 2018-11-16
     * @see
     * @return   Ojbect
     * @params  []
     */
    @GetMapping("/api/admin/index/chart")
    public Object adminHomeChart() {

        List<ChartVo> histogram = checkBoxMapper.getAdminHistogramChartVo();
        List<ChartVo> pieChart = checkBoxMapper.getAdminPieChartVo();

        HomepageNeedsVo chart = new HomepageNeedsVo(histogram, pieChart);

        if (null == chart) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, chart, "操作成功");
        }
    }

    /**
     * @Description 普通用户的：自身设备的三级联动
     * @author mzy
     * @time 2018-11-18
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/equipment/threeLevelLinkage")
    public Object userEquipmentThreeLevelLinkage(HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("user");
        String userNo = userVo.getUserNo();
        // 注意：这里的userId，应该从session中取出来！
        // session.get...
        LinkedList<EquipmentCategoryVo> equipmentThreeLevelLinkageVo =
                checkBoxService.getUserEquipmentThreeLevelLinkageVo(userNo); // 50830025

        if (null == equipmentThreeLevelLinkageVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentThreeLevelLinkageVo, "操作成功");
        }
    }

    /**
     * @Description 管理员用户的：所有设备的三级联动
     * @author mzy
     * @time 2018-11-18
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/equipment/threeLevelLinkage")
    public Object userEquipmentThreeLevelLinkage() {
        LinkedList<EquipmentCategoryVo> equipmentThreeLevelLinkageVo =
                checkBoxService.getAdminEquipmentThreeLevelLinkageVo();

        if (null == equipmentThreeLevelLinkageVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentThreeLevelLinkageVo, "操作成功");
        }
    }

    /**
     * @Description 获取所有用户的二级联动
     * @author mzy
     * @time 2018-11-18
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/twoLevelLinkage/user")
    public Object userTwoLevelLinkage() {
        LinkedList<UserLinkageVo> userTwoLevelLinkageVo =
                checkBoxService.getUserTwoLevelLinkageVo();

        if (null == userTwoLevelLinkageVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, userTwoLevelLinkageVo, "操作成功");
        }

    }

    /**
     * @Description 获取当前时间，放着玩儿(只要日期的)
     * @author mzy
     * @time 2018-11-18
     * @see
     * @return
     * @params
     */
    @GetMapping("/api/get/time")
    public Object getNowTime(){
        return JsonData.buildSuccess(0, DateUtil.formatDate(new Date()), "操作成功");
    }

}
